package com.xtianzhuang.www.utils;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public abstract class SingletonFactory {
	private static final Object LOCK = new Object();
	protected static final String IMPLEMENT_METHOD = "getInstance";
	private static Map<String, Object> instances = new HashMap();

	public SingletonFactory() {
	}

	public static <T> T getInstance(Class<T> cls) {
		if (!instances.containsKey(cls.getName())) {
			synchronized (LOCK) {
				if (!instances.containsKey(cls.getName())) {
					T instance = genInstance(cls);
					instances.put(cls.getName(), instance);
				}
			}
		}

		return (T) instances.get(cls.getName());
	}

	private static <T> T genInstance(Class<T> cls) {
		Object instance = null;

		try {
			Method method = cls.getDeclaredMethod("getInstance");
			instance = method.invoke(cls);
		} catch (IllegalArgumentException var7) {
		} catch (IllegalAccessException var8) {
		} catch (InvocationTargetException var9) {
		} catch (SecurityException var10) {
		} catch (NoSuchMethodException var11) {
			try {
				instance = cls.newInstance();
			} catch (InstantiationException var5) {
			} catch (IllegalAccessException var6) {
			}
		}

		return (T) instance;
	}
}